/*************************************************************************
 * File Name:    Maximum_Depth_of_Binary_Tree.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/4 14:12:09
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Maximum Depth of Binary Tree
 | Given a binary tree, find its maximum depth.
 |
 | The maximum depth is the number of nodes along the longest path from the
 | root node down to the farthest leaf node.
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode *root)
    {
        int ans = 0;
        dfs(root, 0, ans);
        return ans;
    }
    
    void dfs(TreeNode *root, int depth, int &ans)
    {
        if (root == NULL) {
            ans = max(ans, depth);
            return;
        }
        dfs(root->left, depth + 1, ans);
        dfs(root->right, depth + 1, ans);
    }
};

